package com.bolt.admin.web.view.freemarker;


import com.bolt.admin.security.shiro.AuthContextHolder;
import com.bolt.admin.security.shiro.AuthUserDetails;
import com.bolt.common.utils.StrUtil;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;

/**
 * Created by Administrator on 2018/8/3.
 */
public class UserInfoMethodModel implements TemplateMethodModelEx {

    private static final Log logger = LogFactory.getLog(UserInfoMethodModel.class);
    @Override
    public Object exec(List arguments) throws TemplateModelException {
        if ((arguments != null) && (!arguments.isEmpty())
                && (arguments.get(0) != null)
                && (StrUtil.isNotEmpty(arguments.get(0).toString()))) {
            try {
                String str1 = arguments.get(0).toString();
                AuthUserDetails userDetails = AuthContextHolder.getAuthUserDetails();
                String value = BeanUtils.getProperty(userDetails, str1);
                return new SimpleScalar(value);
            } catch (Exception ex) {
                logger.error("current user error",ex);
            }
        }
        return null;
    }
}
